<?php   if (!defined('BASEPATH')) exit('No direct script access allowed');


class Categorie_model extends CI_Model {
	
    function __construct()
    {
        parent::__construct();
    }

    function delete($where)
	{
        $this->db->where($where);
		$this->db->delete('categorie');
        $true = $this->db->affected_rows() > 0; 
		return $true;
	}
    
    function save($data)
    {
        
   		if(!isset($data['id']) || !$data['id'])
        {
            $query = $this->db->query("SELECT max(id) as id FROM categorie");
            $row = $query->row_array();
            $data['id'] = $row['id']+1;
            
            $this->db->insert('categorie', $data);
        }
        else
        {
            $this->db->where('id', $data['id']);
			
            $this->db->update('categorie', $data);
        }
		return $data['id'];

    }

	
    function get_columns()
	{
		$sql = "SELECT column_name from information_schema.columns where
table_name='" . $this->db->dbprefix('categorie') . "'";
		$query = $this->db->query($sql);
		
		return $query->result_array();
	}
	


	function get_list($params = array())
	{
		$default_params = array
		(
			'select' => 'c.*, s.id as sid, s.nome as sottocategoria, s.attivo',
			'order_by' => 'c.nome, s.nome',
			'limit' => 100,
			'where' => null,
			'start' => 0,
		);
		
		foreach ($default_params as $key => $value)
		{
			$params[$key] = (isset($params[$key]))? $params[$key]: $default_params[$key];
		}
	
		$this->db->select($params['select']);
		$this->db->from('categorie as c');
		
		$this->db->join('sottocategorie as s', 's.id_categoria = c.id', 'left');
		
		
		if(!is_null($params['where']))
		{
			$this->db->where($params['where']);
		}
		$this->db->order_by($params['order_by']);
		
		$this->db->limit($params['limit'], $params['start']);
		
		$query = $this->db->get();
		return $query->result_array();
	}


	
	
}